from django.shortcuts import render

# Create your views here.

from  django.contrib.auth.backends import ModelBackend

from django.db.models import Q

from django.contrib.auth import get_user_model

from rest_framework.mixins import CreateModelMixin

from rest_framework import viewsets

from .serializers import UserRegSerializer

User = get_user_model()

class CustomBackend(ModelBackend):
    """
      自定义用户验证
    """
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            user = User.objects.get(Q(username=username) | Q(mobile=username))
            if user.check_password(password):
                return user
        except Exception as e:
            return None


class UserRegViewSet(CreateModelMixin,viewsets.GenericViewSet):
    serializer_class = UserRegSerializer